package com.longkey.workflow.client.canvas
{
	import com.longkey.workflow.client.constants.ElementType;
	import com.longkey.workflow.client.constants.StatusType;
	import com.longkey.workflow.client.util.IDGenerator;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.LinkButton;
	import mx.core.UIComponent;
	import mx.graphics.SolidColor;
	
	import spark.components.BorderContainer;
	import spark.components.Button;
	import spark.components.ToggleButton;
	import spark.layouts.HorizontalLayout;
	import spark.primitives.BitmapImage;
	
	public class WFControlBar extends BorderContainer
	{
		private static var selectImg:BitmapImage;
		
		[Embed(source="/img/controlbar/sticky24.png")]
		private static var stickyIcon : Class;
		
		[Embed(source="/img/controlbar/select24.png")]
		private static var selectIcon : Class;
		
		[Embed(source="/img/controlbar/start24.png")]
		private static var startIcon : Class;
		
		[Embed(source="/img/controlbar/task24.png")]
		private static var taskIcon : Class;
		
		[Embed(source="/img/controlbar/proc24.png")]
		private static var procIcon : Class;
		
		[Embed(source="/img/controlbar/end24.png")]
		private static var endIcon : Class;
		
		[Embed(source="/img/controlbar/link24.png")]
		private static var linkIcon : Class;
		
		private var canvas:WFCanvas;
		
		private var sticky:ToggleButton;
		private var select:ToggleButton;
		private var start:ToggleButton;
		private var task:ToggleButton;
		private var proc:ToggleButton;
		private var end:ToggleButton;
		private var link:ToggleButton;
		
		private var controls:ArrayCollection;
		
		public var createElementType:int = ElementType.NOP;
		
		public function WFControlBar(canvas:WFCanvas)
		{
			this.canvas = canvas;
			var hl:HorizontalLayout = new HorizontalLayout();
			hl.paddingLeft = 10;
			hl.verticalAlign = "middle";
			this.layout = hl;
			this.height = 26;
			this.percentWidth = 100;
			this.backgroundFill = new SolidColor(0xDFE8DF);
			this.controls = new ArrayCollection();
			init();
		}
		
		private function init():void{
			sticky = new ToggleButton();
			sticky.id = "sticky";
			sticky.setStyle("icon", stickyIcon);
			sticky.width = 24;
			sticky.height = 24;
			sticky.toolTip = "粘滞";
			sticky.setStyle("skinClass", com.longkey.workflow.client.canvas.ControlToggleButtonSkin);
			sticky.addEventListener(MouseEvent.CLICK, onStickyBtnClick);
			
			select = new ToggleButton();
			select.id = "select";
			select.setStyle("icon", selectIcon);
			select.width = 24;
			select.height = 24;
			select.toolTip = "选择";
			select.setStyle("skinClass", com.longkey.workflow.client.canvas.ControlToggleButtonSkin);
			select.selected = true;
			select.addEventListener(MouseEvent.CLICK, onSelectBtnClick);

			start = new ToggleButton();
			start.id = "start";
			start.setStyle("icon", startIcon);
			start.width = 24;
			start.height = 24;
			start.toolTip = "开始";
			start.setStyle("skinClass", com.longkey.workflow.client.canvas.ControlToggleButtonSkin);
			start.addEventListener(MouseEvent.CLICK, onControlBtnClick);
			
			task = new ToggleButton();
			task.id = "task";
			task.setStyle("icon", taskIcon);
			task.width = 24;
			task.height = 24;
			task.toolTip = "任务";
			task.setStyle("skinClass", com.longkey.workflow.client.canvas.ControlToggleButtonSkin);
			task.addEventListener(MouseEvent.CLICK, onControlBtnClick);
			
			
			proc = new ToggleButton();
			proc.id = "proc";
			proc.setStyle("icon", procIcon);
			proc.width = 24;
			proc.height = 24;
			proc.toolTip = "子流程";
			proc.setStyle("skinClass", com.longkey.workflow.client.canvas.ControlToggleButtonSkin);
			proc.addEventListener(MouseEvent.CLICK, onControlBtnClick);
			
			end = new ToggleButton();
			end.id = "end";
			end.setStyle("icon", endIcon);
			end.width = 24;
			end.height = 24;
			end.toolTip = "结束";
			end.setStyle("skinClass", com.longkey.workflow.client.canvas.ControlToggleButtonSkin);
			end.addEventListener(MouseEvent.CLICK, onControlBtnClick);
			
			link = new ToggleButton();
			link.id = "link";
			link.setStyle("icon", linkIcon);
			link.width = 24;
			link.height = 24;
			link.toolTip = "连线";
			link.setStyle("skinClass", com.longkey.workflow.client.canvas.ControlToggleButtonSkin);
			link.addEventListener(MouseEvent.CLICK, onControlBtnClick);
			
			this.addElement(sticky);
			this.addElement(select);
			this.addElement(start);
			this.addElement(task);
			this.addElement(proc);
			this.addElement(end);
			this.addElement(link);
			
//			this.controls.addItem(select);
			this.controls.addItem(start);
			this.controls.addItem(task);
			this.controls.addItem(proc);
			this.controls.addItem(end);
			this.controls.addItem(link);
		}
		
		private function onControlBtnClick(event:MouseEvent):void{
			var lb:ToggleButton = event.currentTarget as ToggleButton;
			var selectId:String = lb.id;
			for(var i:int = 0; i < this.controls.length; i++){
				var btn:ToggleButton = this.controls.getItemAt(i) as ToggleButton;
				if(btn.id != selectId){
					btn.selected = false;
				}
			}
			if(lb.selected){
				this.select.selected = false;
				canvas.status = StatusType.CANVAS_CREATE_ELEMENT;
			}
			else{
				if(!this.sticky.selected)
					this.select.selected = true;
				canvas.status = StatusType.CANVAS_NORMAL;
			}
			
			this.setCreateElementType();
		}
		
		private function onSelectBtnClick(event:MouseEvent):void{
			this.select.selected = true;
			this.sticky.selected = false;
			for(var i:int = 0; i < this.controls.length; i++){
				var btn:ToggleButton = this.controls.getItemAt(i) as ToggleButton;
				btn.selected = false;
			}
			canvas.status = StatusType.CANVAS_NORMAL;
			this.setCreateElementType();
		}
		
		private function onStickyBtnClick(event:MouseEvent):void{
			canvas.status = StatusType.CANVAS_NORMAL;
			if(this.sticky.selected){
				this.select.selected = false;
				for(var i:int = 0; i < this.controls.length; i++){
					var btn:ToggleButton = this.controls.getItemAt(i) as ToggleButton;
					if(btn.selected){
						canvas.status = StatusType.CANVAS_CREATE_ELEMENT;
						break;
					}
				}
			}
			else{
				this.select.selected = true;
				for(i = 0; i < this.controls.length; i++){
					btn = this.controls.getItemAt(i) as ToggleButton;
					btn.selected = false;
				}
			}
			this.setCreateElementType();
		}
		
		public function refresh():void{
			canvas.status = StatusType.CANVAS_NORMAL;
			if(this.sticky.selected){
				this.select.selected = false;
				for(var i:int = 0; i < this.controls.length; i++){
					var btn:ToggleButton = this.controls.getItemAt(i) as ToggleButton;
					if(btn.selected){
						canvas.status = StatusType.CANVAS_CREATE_ELEMENT;
						break;
					}
				}
			}
			else{
				this.select.selected = true;
				for(i = 0; i < this.controls.length; i++){
					btn = this.controls.getItemAt(i) as ToggleButton;
					btn.selected = false;
				}
			}
			this.setCreateElementType();
		}
		
		public function isSticky():Boolean{
			return this.sticky.selected;
		}
		
		private function setCreateElementType():void{
			var type:int = ElementType.NOP;
			for(var i:int = 0; i < this.controls.length; i++){
				var btn:ToggleButton = this.controls.getItemAt(i) as ToggleButton;
				if(btn.selected){
					switch(btn.id){
						case "start":
							type = ElementType.START;
//							canvas.createTempElement(type);
							break;
						case "task":
							type = ElementType.TASK;
//							canvas.createTempElement(type);
							break;
						case "proc":
							type = ElementType.PROC;
//							canvas.createTempElement(type);
							break;
						case "end":
							type = ElementType.END;
//							canvas.createTempElement(type);
							break;
						case "link":
							type = ElementType.LINK;
							break;
						default:
//							canvas.clearTempElement();
							break;
					}
					break;
				}
			}
			this.createElementType = type;
		}
	}
}